<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use layui\Layui;

class Type extends Base
{
	//分类列表
	public function index()
	{
		$search_name = input('search_name');
        $search = ['query'=>[]];
        $search['query']['search_name'] = $search_name;
        $total = Db::table('type')->count();
		$result = Db::table('type')->field("*,concat(path,',',id) as paths")->order('paths')->where('name','like',"%{$search_name}%")->select(); 
		//分级
		foreach($result as $key=>$value){
			$path=$value['path'];
			//echo $path."<br>";
			//转化为数组
			$array=explode(',',$path);
			//获取数组长度
			$arraylength=count($array);
			//获取逗号个数
			$length=$arraylength-1;
			//重复字符串函数
			$result[$key]['name']=str_repeat("------|",$length).$value['name'];
		}
		$this->assign('search_name',$search_name);
		$this->assign('total',$total);
		$this->assign('result',$result);
		return $this->fetch('type_list');
	}
	//添加分类
	public function add()
	{
		//获取分类信息
		$add = Db::table('type')->field("*,concat(path,',',id) as paths")->order('paths')->select();
		foreach($add as $key=>$value){
			$path=$value['path'];
			//echo $path."<br>";
			//转化为数组
			$array=explode(',',$path);
			//获取数组长度
			$arraylength=count($array);
			//获取逗号个数
			$length=$arraylength-1;
			//重复字符串函数
			$add[$key]['name']=str_repeat("------|",$length).$value['name'];
		}
		$this->assign('add',$add);
		return $this->fetch('type_add');
	}
	//执行添加
	public function insert()
	{
		$pid = (int)input('post.pid');
		if($pid == 0){
			$data['name'] = trim(input('post.name'));
			$data['pid'] = (int)input('post.pid');
			$data['status'] = (int)(input('post.status'));
			$data['path'] = "0"; 
		}else{
			$info = Db::table('type')->where(array('id'=>$pid))->select();
			$data['name'] = trim(input('post.name'));
			$data['pid'] = (int)input('post.pid');
			$data['status'] = (int)(input('post.status'));
			$data['path'] = $info[0]['path'].','.$info[0]['id'];
		}
		$result = Db::table('type')->insert($data);
		if($result){
			exit(json_encode(array('code'=>0,'msg'=>'添加成功')));
		}
	}
	//编辑
	public function edit()
	{
		$id = (int)input('get.id');
		$type = Db::table('type')->find($id);
		$data = Db::table('type')->field("*,concat(path,',',id) as paths")->order('paths')->select();
		foreach($data as $key=>$value){
			$path=$value['path'];
			//echo $path."<br>";
			//转化为数组
			$array=explode(',',$path);
			//获取数组长度
			$arraylength=count($array);
			//获取逗号个数
			$length=$arraylength-1;
			//重复字符串函数
			$data[$key]['name']=str_repeat("------|",$length).$value['name'];
		}
		$this->assign("type",$type);
		$this->assign("data",$data);
		return $this->fetch('type_edit');
	}
	//执行编辑
	public function update()
	{
		$id = (int)input('post.id');
		$data['name'] = trim(input('post.name'));
		$data['pid'] = (int)input('post.pid');
		$data['status'] = (int)(input('post.status'));
		if($data['pid'] == 0){
			$data['path'] = "0";
		}else{
			$info = Db::table('type')->where(array('id'=>$data['pid']))->select();
			$data['path'] = $info[0]['path'].','.$info[0]['id'];
		}
		
		if($id){
			$result = Db::table('type')->where(array("id"=>$id))->update($data);
		}
		exit(json_encode(array('code'=>0,'msg'=>'修改成功')));
	}
	//执行删除
	public function delete()
	{
		$id = (int)input('post.id');
		$res = Db::table('type')->where(array('pid'=>$id))->count();
		if($res>0){
			exit(json_encode(array('code'=>0,'msg'=>'请先删除子分类')));
		}else{
			Db::table('type')->delete($id);
		}
		exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
	}
}